package com.jaya.budan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class My_Wallet_Withdraw2_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private Button btn_login;
    private String access_token, money, limit_money, fee_lv;
    private TextView tv_wallet_withdraw_limit_money_yb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet_withdraw2);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        money = intent.getStringExtra("money");
        limit_money = intent.getStringExtra("limit_money");
        fee_lv = intent.getStringExtra("fee_lv");
        back = findViewById(R.id.back);
        btn_login = findViewById(R.id.btn_login);
        tv_wallet_withdraw_limit_money_yb = findViewById(R.id.tv_wallet_withdraw_limit_money_yb);
        back.setOnClickListener(this);
        btn_login.setOnClickListener(this);
        tv_wallet_withdraw_limit_money_yb.setText("" + money);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                JSONObject object = new JSONObject();
                try {
                    object.put("bd", "");
                    object.put("money", money);
                    object.put("moneyLv", "money*fee_lv");
                    object.put("type", "");
                    object.put("bank_id", "");
                    object.put("payPsd", "");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                postData(object);
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData(JSONObject object) {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, RequestConnect.gr_my_wallet_go_withdraw_Url + access_token, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("wallet", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONObject info = jsonObject.getJSONObject("info");
                        String access_token = info.getString("access_token");
//                        Intent intent = new Intent(MainActivity.this, Register3_Activity.class);//已注册，自动跳转至首页页面
//                        startActivity(intent);
//                        finish();
                    } else {
                        ToolUtils.showToast(My_Wallet_Withdraw2_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(My_Wallet_Withdraw2_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
